package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.config.JWTConfig;
import online.longzipeng.cinemasys.dto.customer.CustomerLoginResponseDTO;
import online.longzipeng.cinemasys.dto.order.OderQueryResponse;
import online.longzipeng.cinemasys.dto.sysuser.SysUserLoginResponseDTO;
import online.longzipeng.cinemasys.entity.OrderHead;
import online.longzipeng.cinemasys.service.OrderHeadService;
import online.longzipeng.cinemasys.vo.order.OrderHeadUpdateVO;
import online.longzipeng.cinemasys.vo.order.OrderQueryVO;
import online.longzipeng.cinemasys.vo.order.OrderSaveVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  订单头控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/order")
@Api(value = "订单相关接口",description = "订单相关接口",tags = "订单相关接口")
public class OrderHeadController {

    @Autowired
    OrderHeadService orderHeadService;
    @Autowired
    JWTConfig jwtConfig;

    @PostMapping("/get-list-page")
    @ApiOperation("条件查询所有订单带分页")
    public Result getListPage(@RequestBody OrderQueryVO queryVO){
        IPage<OderQueryResponse> hallIPage = orderHeadService.getListPage(queryVO);
        return Result.success(hallIPage);
    }

    @GetMapping("/get-list-by-customer-and-status")
    @ApiOperation("根据用户状态查询所有订单")
    public Result getListByCustomerAndStatus(Integer status,@RequestHeader("X-Token")String token){
        CustomerLoginResponseDTO customerLoginResponseDTO = jwtConfig.checkJwtWeb(token);
        List<OderQueryResponse> orders = orderHeadService.getListByCustomerAndStatus(status,customerLoginResponseDTO.getId());
        return Result.success(orders);
    }

    @PostMapping("/save")
    @NeedPermissions("orderHead:save")
    @ApiOperation("新增订单头")
    public Result save(@Valid @RequestBody OrderSaveVO orderSaveVO,@RequestHeader("X-Token")String token){
        CustomerLoginResponseDTO customerLoginResponseDTO = jwtConfig.checkJwtWeb(token);
        orderSaveVO.setCustomerId(customerLoginResponseDTO.getId());
        String orderNum = orderHeadService.add(orderSaveVO);
        return Result.success(orderNum,"订单添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("orderHead:delete")
    @ApiOperation("删除订单头")
    public Result deleteOne(@ApiParam(value = "订单头id",required = true)Long id){
        try {
            orderHeadService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"订单头删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("orderHead:delete")
    @ApiOperation("删除多个订单头")
    public Result deleteOne(@ApiParam(value = "订单头id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            orderHeadService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"订单头删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("orderHead:update")
    @ApiOperation("修改订单头")
    public Result update(@Valid @RequestBody OrderHeadUpdateVO orderHeadUpdateVO) {
        OrderHead orderHead = new OrderHead();
        BeanUtils.copyProperties(orderHeadUpdateVO,orderHead);
        orderHeadService.updateOrderHead(orderHead);
        return Result.success(null,"订单头修改成功！！");
    }

}
